<?php
/*
 * 进云智能生成平台
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
 */

defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['setting']=$this->plugin_setting('basic');
$uniacid=$_SESSION['uniacid'];
if($_RQ['op']=='main'){
	$last_month = date('Y-m');
	//检查账单是否生成，如果没有，则生成所有上月账单！
	$fee1=pdo_get('jy_smallquan_fee',array('hid'=>$_RQ['hid'],'uniacid'=>$_SESSION['uniacid'],'time'=>$last_month));
	if(empty($fee1)){
		$all=pdo_getall('jy_smallquan_fee',array('uniacid'=>$_SESSION['uniacid'],'time'=>$last_month));
		if(empty($all)){
			$houses=pdo_getall('jy_smallquan_house',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1'));
			$fee_classify=pdo_getall('jy_smallquan_fee_classify',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1'));
			if($houses){
				$xiangxi=array();
				foreach($houses as $house){
					foreach($fee_classify as $classify){
						if($classify['type']==1){
							$project=array('title'=>$classify['title'],'fee'=>$house['area']*$classify['fee']);
						}else{
							$project=array('title'=>$classify['title'],$classify['fee']);
						}
						$fee+=$project['fee'];
						$xiangxi[]=$project;
					}
					//查询户主
					$huzhu=pdo_get('jy_smallquan_household',array('uniacid'=>$_SESSION['uniacid'],'hid'=>$house['id']));
					$insert=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$_SESSION['uid'],
						'zid'=>$huzhu['id'],
						'hid'=>$house['id'],
						'time'=>$last_month,
						'fee'=>$fee,
						'xiangxi'=>ijson_encode($xiangxi),
						'createtime'=>TIMESTAMP,
					);
					pdo_insert('jy_smallquan_fee',$insert);
				}
			}
		}
	}
	//查询我的账单
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if($_RQ['hid']){
		$params['hid']=$_RQ['hid'];
	}
	$house=pdo_get('jy_smallquan_house',array('id'=>$_RQ['hid']));
	$house['building']=pdo_get('jy_smallquan_building',array('uniacid'=>$_SESSION['uniacid'],'id'=>$house['bid']));
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','zid','mid','fee','hid','time','xiangxi','ispay','paytime','status','createtime');
	$orders=pdo_getslice('jy_smallquan_fee',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($orders){
		foreach($orders as &$item){
			$item['createtime']=date('Y-m-d',$item['createtime']);
			$item['paytime']=date('Y-m-d h:i',$item['paytime']);
			$item['xiangxi']=json_decode($item['xiangxi'],true);
			$item['starttime']=$item['time'].'-1';
			$timestamp = strtotime( '2018-3' );
			$mdays = date( 't', $timestamp );
			$item['endtime']=$item['time'].'-'.$mdays;
		}
	}
	$return['orders']=$orders;
	$return['house']=$house;
	//print_r($houses);exit;
	exi($return);
}

if($_RQ['op']=='pay'){
	$post=get_postdata();
	$id=$post['id'];
	if(!$id){
		exi('账单不存在，请重新选择账单','error');
	}
	$order=pdo_get('jy_smallquan_fee',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$params['fee']=$order['fee'];
	$params['title']='支付'.$order['time'].'账单';
	$params['id']=$id;
	$params['plugin']='jy_smallquan';
	$params['back_url']='jy_smallquan/myorder/payresult';
	$return['payid']=cfc('pay')->create_pay_order($params);
	exi($return);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','list');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','list');
	}
	$order=pdo_get('jy_smallquan_fee',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定账单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！账单金额不匹配！','error');
	}
	pdo_update('jy_smallquan_fee',array('status'=>1,'ispay'=>1,'paytime'=>TIMESTAMP),array('id'=>$order['id']));
	exi('支付成功','success','jy_smallquan/myorder//ispay=1');
}		
		


